/*
 * Copyright (c) 2020 Fraunhofer FOKUS and others. All rights reserved.
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contact: mosaic@fokus.fraunhofer.de
 */
package org.eclipse.mosaic.fed.carla.clojure.commands;

import libpython_clj2.java_api;
import org.eclipse.mosaic.fed.carla.clojure.AbstractCarlaCommand;
import org.eclipse.mosaic.fed.carla.clojure.ClojureCarlaConnection;

public class VehicleRemove extends AbstractCarlaCommand {

    public VehicleRemove() {
        super();
    }

    public void execute(ClojureCarlaConnection connection, String sumoVehicleId) {
        try (AutoCloseable locker = java_api.GILLocker()) {
            if (connection.sumo2carlaIds.containsKey(sumoVehicleId)) {
                long carlaActorId = connection.sumo2carlaIds.remove(sumoVehicleId);
                Object carlaActor = getCarlaActor(connection, carlaActorId);

                if (carlaActor != null) {
                    Object destroyAttr = java_api.getAttr(carlaActor, "destroy");
                    java_api.call(destroyAttr);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Vehicle remove failed when request carla. ", e);
        }
    }
}
